<template>
  <div class="report-container">
    <!-- 头部标题 -->
    <div class="report-header">
      <h1>心理健康测评报告</h1>
      <p class="report-time">报告生成时间：{{ reportTime }}</p>
    </div>

    <!-- 总体得分卡片 -->
    <div class="overall-score">
      <div class="score-circle">
        <div class="score-value">{{ totalScore }}</div>
        <div class="score-label">综合得分</div>
      </div>
      <div class="score-description">
        <h3>总体评估</h3>
        <p v-html="overallDescription"></p>
      </div>
    </div>

    <!-- 维度分析 -->
    <div class="dimension-analysis">
      <h2>维度分析</h2>
      <div class="dimension-cards">
        <div
          v-for="(dimension, index) in dimensionData"
          :key="dimension.entityId"
          class="dimension-card"
          :class="getDimensionClass(dimension)"
        >
          <div class="dimension-header">
            <h3>{{ dimension.entityName }}</h3>
            <div class="dimension-score">
              <span class="score">{{ dimension.score }}</span>
              <span class="total"
                >/{{ getTotalScore(dimension.entityName) }}</span
              >
            </div>
          </div>
          <div class="dimension-progress-container">
            <div
              class="progress-bar"
              :style="{ width: getProgressWidth(dimension) }"
            ></div>
            <div class="progress-label">
              <span>低</span>
              <span>高</span>
            </div>
          </div>
          <div class="dimension-content">
            <div class="explain-section">
              <h4>维度解释</h4>
              <p v-html="dimension.entityExplain"></p>
            </div>
            <div class="advice-section">
              <h4>专业建议</h4>
              <p v-html="dimension.entityAd"></p>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 总结与建议 -->
    <div class="summary-section">
      <h2>总结与建议</h2>
      <div class="summary-content">
        <p>
          根据您的测评结果，我们为您提供了以上针对性的建议。请结合自身情况，有选择性地采纳这些建议，逐步改善您的心理健康状况。
        </p>
        <p>
          如果您对测评结果有任何疑问，或需要进一步的专业帮助，请及时联系心理健康专业人士。
        </p>
      </div>
    </div>

    <!-- 操作按钮 -->
    <div class="action-buttons">
      <button class="btn btn-secondary" @click="goBack">返回打卡</button>
      <button class="btn btn-secondary" @click="gohome">返回首页</button>
    </div>
  </div>
</template>

<script>
import { getLbassessmentReport } from "../../api/user";

export default {
  name: "AssessmentReport",
  data() {
    return {
      reportData: [],
      reportTime: new Date().toLocaleDateString("zh-CN"),
    };
  },
  computed: {
    // 获取心理健康总分
    totalScore() {
      const totalDimension = this.reportData.find(
        (item) => item.entityName === "心理健康总分"
      );
      return totalDimension ? totalDimension.score : 0;
    },
    // 获取总体描述
    overallDescription() {
      const totalDimension = this.reportData.find(
        (item) => item.entityName === "心理健康总分"
      );
      return totalDimension ? totalDimension.entityExplain : "";
    },
    // 获取维度数据（排除总分）
    dimensionData() {
      return this.reportData.filter(
        (item) => item.entityName !== "心理健康总分"
      );
    },
  },
  created() {
    // 获取路由参数
    this.id = this.$route.query.id;
    this.getReportData();
  },
  methods: {
    getReportData() {
      let data = { Id: this.id };
      getLbassessmentReport(data)
        .then((res) => {
          if (res.code === 200) {
            this.reportData = res.data;
          } else {
            console.error("获取报告失败:", res.message);
          }
        })
        .catch((error) => {
          console.error("请求报告出错:", error);
        });
    },
    // 根据维度名称获取总分
    getTotalScore(dimensionName) {
      const scoreMap = {
        自我肯定: 8,
        抑郁: 6,
        焦虑: 6,
        心理健康总分: 20,
      };
      return scoreMap[dimensionName] || 10;
    },
    // 获取进度条宽度
    getProgressWidth(dimension) {
      const total = this.getTotalScore(dimension.entityName);
      const percentage = (dimension.score / total) * 100;
      // 确保进度条宽度不会超过100%
      return `${Math.min(percentage, 100)}%`;
    },
    // 根据得分获取维度卡片样式类
    getDimensionClass(dimension) {
      const total = this.getTotalScore(dimension.entityName);
      const ratio = dimension.score / total;

      if (ratio <= 0.4) {
        return "low-score";
      } else if (ratio <= 0.7) {
        return "medium-score";
      } else {
        return "high-score";
      }
    },

    // 返回首页
    goBack() {
      this.$router.go(-2);
    },
    gohome() {
      this.$router.go(-3);
    },
  },
};
</script>

<style scoped>
/* 添加基础样式重置 */
* {
  box-sizing: border-box;
}

.report-container {
  margin: 0 auto;
  padding: 20px;
  font-family: "PingFang SC", "Microsoft YaHei", sans-serif;
  background-color: #f8fbff;
  min-height: 100vh;
  max-width: 100%;
  overflow-x: hidden; /* 防止横向滚动 */
}

.report-header {
  text-align: center;
  margin-bottom: 30px;
  padding-bottom: 20px;
  border-bottom: 1px solid #e1ecff;
}

.report-header h1 {
  color: #1a3b5d;
  margin-bottom: 10px;
  font-size: 28px;
  word-wrap: break-word;
}

.report-time {
  color: #6c757d;
  font-size: 14px;
}

/* 总体得分样式 */
.overall-score {
  display: flex;
  background: white;
  border-radius: 12px;
  padding: 30px;
  margin-bottom: 30px;
  box-shadow: 0 4px 15px rgba(77, 171, 247, 0.1);
  align-items: center;
  max-width: 100%;
}

.score-circle {
  flex-shrink: 0;
  width: 150px;
  height: 150px;
  border-radius: 50%;
  background: linear-gradient(135deg, #4dabf7, #339af0);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  color: white;
  margin-right: 30px;
}

.score-value {
  font-size: 42px;
  font-weight: bold;
  line-height: 1;
}

.score-label {
  font-size: 16px;
  margin-top: 5px;
}

.score-description {
  flex: 1;
  min-width: 0; /* 防止flex项目溢出 */
}

.score-description h3 {
  color: #1a3b5d;
  margin-bottom: 15px;
  font-size: 20px;
}

.score-description p {
  line-height: 1.6;
  color: #495057;
  word-wrap: break-word;
}

/* 维度分析样式 */
.dimension-analysis {
  margin-bottom: 30px;
  max-width: 100%;
}

.dimension-analysis h2 {
  color: #1a3b5d;
  margin-bottom: 20px;
  font-size: 24px;
  padding-bottom: 10px;
  border-bottom: 1px solid #e1ecff;
}

.dimension-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 20px;
  width: 100%;
}

.dimension-card {
  background: white;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.05);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  max-width: 100%;
  overflow: hidden; /* 防止内容溢出 */
}

.dimension-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
}

.dimension-card.low-score {
  border-left: 4px solid #ff6b6b;
}

.dimension-card.medium-score {
  border-left: 4px solid #ffa94d;
}

.dimension-card.high-score {
  border-left: 4px solid #51cf66;
}

.dimension-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
  flex-wrap: wrap; /* 在小屏幕上允许换行 */
}

.dimension-header h3 {
  color: #1a3b5d;
  font-size: 18px;
  margin: 0;
  word-break: break-word;
}

.dimension-score {
  display: flex;
  align-items: baseline;
  flex-shrink: 0;
}

.dimension-score .score {
  font-size: 24px;
  font-weight: bold;
  color: #339af0;
}

.dimension-score .total {
  font-size: 14px;
  color: #6c757d;
  margin-left: 2px;
}

/* 修复进度条样式 */
.dimension-progress-container {
  margin-bottom: 20px;
  width: 100%;
  position: relative;
}

.progress-bar {
  height: 8px;
  background: linear-gradient(90deg, #4dabf7, #339af0);
  border-radius: 4px;
  margin-bottom: 5px;
  transition: width 1s ease;
  max-width: 100%; /* 确保进度条不会超出容器 */
}

.progress-label {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: #6c757d;
  width: 100%;
}

.dimension-content h4 {
  color: #1a3b5d;
  margin: 15px 0 8px;
  font-size: 16px;
}

.dimension-content p {
  line-height: 1.6;
  color: #495057;
  font-size: 14px;
  word-wrap: break-word; /* 防止长文本溢出 */
}

.explain-section,
.advice-section {
  margin-bottom: 15px;
}

/* 总结与建议样式 */
.summary-section {
  background: white;
  border-radius: 12px;
  padding: 25px;
  margin-bottom: 30px;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.05);
  max-width: 100%;
}

.summary-section h2 {
  color: #1a3b5d;
  margin-bottom: 15px;
  font-size: 24px;
}

.summary-content p {
  line-height: 1.6;
  color: #495057;
  margin-bottom: 10px;
  word-wrap: break-word;
}

/* 操作按钮样式 */
.action-buttons {
  display: flex;
  justify-content: center;
  gap: 15px;
  margin-top: 30px;
  flex-wrap: wrap; /* 小屏幕上按钮换行 */
}

.btn {
  padding: 12px 30px;
  border: none;
  border-radius: 6px;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s ease;
  font-weight: 500;
  min-width: 120px;
}

.btn-primary {
  background: linear-gradient(135deg, #4dabf7, #339af0);
  color: white;
}

.btn-primary:hover {
  background: linear-gradient(135deg, #339af0, #228be6);
  box-shadow: 0 4px 12px rgba(51, 154, 240, 0.3);
}

.btn-secondary {
  background: #40a2f4;
  color: #fff;
  border: 1px solid #dee2e6;
}

.btn-secondary:hover {
  background: #e9ecef;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .report-container {
    padding: 15px;
  }

  .report-header h1 {
    font-size: 24px;
  }

  .overall-score {
    flex-direction: column;
    text-align: center;
    padding: 20px;
  }

  .score-circle {
    margin-right: 0;
    margin-bottom: 20px;
    width: 120px;
    height: 120px;
  }

  .score-value {
    font-size: 36px;
  }

  .dimension-cards {
    grid-template-columns: 1fr;
    gap: 15px;
  }

  .dimension-card {
    padding: 15px;
  }

  .dimension-header {
    flex-direction: column;
    align-items: flex-start;
  }

  .dimension-score {
    margin-top: 8px;
  }

  .action-buttons {
    flex-direction: center;
    align-items: center;
  }

  .btn {
    /* width: 100%; */
    max-width: 280px;
  }
}

@media (max-width: 480px) {
  .report-container {
    padding: 10px;
  }

  .overall-score {
    padding: 15px;
  }

  .score-circle {
    width: 100px;
    height: 100px;
  }

  .score-value {
    font-size: 32px;
  }

  .score-label {
    font-size: 14px;
  }

  .dimension-card {
    padding: 12px;
  }

  .summary-section {
    padding: 20px;
  }
}
</style>
